<template>
  <div style="width:100%">
    <a-card>
      <a-form layout="Vertical">
        <a-row :gutter="48">
          <!-- <a-col :md="8" :sm="12">
            <a-form-item label="培养单位"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}">
              <a-select v-model="searchParams.ssyxm" showSearch :filterOption="filterOption" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="专业"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}">
              <a-select v-model="searchParams.sszym" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.zyList" :key="index" :value="i.zyh">{{i.zyh}}{{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="24">
            <a-form-item
              label="培养模式"
            >
              <a-select labelInValue v-model="searchParams.pymsm" @change="pymsChange">
                <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" v-if="searchParams.pymsm.key === '01'||searchParams.pymsm.key === '03'">
            <a-form-item
              label="一级学科"

            >
              <a-select labelInValue showSearch :filterOption="filterOption" v-model="searchParams.xkdm">
                <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{ i.xkdm + ' ' +
                  i.xkmc }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" v-if="searchParams.pymsm.key === '02'">
            <a-form-item
              label="专业领域（类别）"

            >
              <a-select labelInValue showSearch :filterOption="filterOption" v-model="searchParams.xkdm">
                <a-select-option v-for="i in selectMenu.subjectList" :key="i.id" :value="i.xkdm">{{ i.xkdm + ' ' + i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item
              label="培养类别"

            >
              <a-select labelInValue v-model="searchParams.xslbm">
                <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="8" :sm="24">
            <a-form-item
              label="培养来源"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}"
            >
              <a-select labelInValue v-model="searchParams.xslx">
                <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="24">
            <a-form-item
              label="年级"

            >
              <a-select labelInValue v-model="searchParams.ssnj">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col style="float: right;text-align: center;" :md="24" :sm="24">
            <a-button type="primary" @click="searchScheme" style="margin:0 10px">查询</a-button>
          </a-col>
        </a-row>
        <a-row :gutter="48">
          <a-col :md="12" :sm="24" style="margin:10px 0">
            <a-form-item
              label="源培养方案"

            >
              <a-select v-model="searchParams.scheIndex" @change="schemeChange" >
                <a-select-option v-for="(i, index) in schemeList" :key="i.id" :value="index">{{ i.pyfamc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    <a-card style="margin-top:50px">
      <a-form :form="form">
        <a-row :gutter="48">
          <!-- <a-col :md="8" :sm="12">
            <a-form-item label="院系"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}">
              <a-select v-decorator="['targetYx',
                {
                  rules: [{ required: true, message: '请选择院系!' }],
                }]" labelInValue @change="e => handleChange(e, 'yx')" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="专业"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}">
              <a-select v-decorator="['targetZy',
                {
                  rules: [{ required: true, message: '请选择专业!' }],
                }]" labelInValue @change="e => handleChange(e)" showSearch :filterOption="filterOption">
                <a-select-option v-for="(i,index) in selectMenu.targetZyList" :key="index" :value="i.zyh">{{i.zyh}}{{ i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="24">
            <a-form-item
              label="培养模式"

            >
              <a-select disabled labelInValue @change="e => handleChange(e, 'pyms')" v-decorator="['targetPyms',
                {
                  rules: [{ required: true, message: '请选择培养模式!' }],
                }]">
                <a-select-option v-for="i in TYPE0347" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" v-if="targetParams.pymsm.key === '01'||targetParams.pymsm.key === '03'">
            <a-form-item
              label="一级学科"

            >
              <a-select disabled showSearch @change="e => handleChange(e)" labelInValue :filterOption="filterOption"
                        v-decorator="['targetXkdm',
                {
                  rules: [{ required: true, message: '请选择一级学科!' }],
                }]">
                <a-select-option v-for="i in selectMenu.targetSubjectList" :key="i.id" :value="i.xkdm">{{ i.xkdm + ' ' +
                  i.xkmc }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24" v-if="targetParams.pymsm.key === '02'">
            <a-form-item
              label="专业领域（类别）"

            >
              <a-select disabled showSearch @change="e => handleChange(e)" labelInValue :filterOption="filterOption" v-decorator="['targetXkdm',
                {
                  rules: [{ required: true, message: '请选择专业领域（类别）!' }],
                }]">
                <a-select-option v-for="i in selectMenu.targetSubjectList" :key="i.id" :value="i.xkdm">{{ i.xkdm + ' ' + i.xkmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item
              label="培养类别"

            >
              <a-select disabled labelInValue @change="e => handleChange(e)" v-decorator="['targetXslb',
                {
                  rules: [{ required: true, message: '请选择培养类别!' }],
                }]">
                <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <!--          <a-col :md="6" :sm="24">-->
          <!--            <a-form-item-->
          <!--              label="培养来源"-->
          <!--              -->
          <!--            >-->
          <!--              <a-select disabled labelInValue @change="e => handleChange(e)" v-decorator="['targetXslx',-->
          <!--                {-->
          <!--                  rules: [{ required: true, message: '请选择培养类型!' }],-->
          <!--                }]">-->
          <!--                <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>-->
          <!--              </a-select>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->
          <a-col :md="6" :sm="24">
            <a-form-item
              label="年级"

            >
              <a-select labelInValue @change="e => handleChange(e)" v-decorator="['targetSsnj',
                {
                  rules: [{ required: true, message: '请选择年级!' }],
                }]">
                <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="培养方案"

            >
              <a-input v-decorator="['targetPyfamc',
                {
                  rules: [{ required: true, message: '请输入培养方案名称!' }],
                }]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <div style="color: #E51C23">温馨提示，请尽量按照规则：年级（如：2014级）+ 培养来源名称 + 学科/领域名称 + 培养类别名称 + 一级学科/专业学位类别（领域） + “培养方案”，进行培养方案命名。</div>
      </a-form>
    </a-card>
    <div class="ant-pro-footer-toolbar">
      <a-button style="float: right;margin-top: 10px;" @click="submit" :loading="loadingBtn" type="primary">确认复制</a-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data () {
    return {
      selectMenu: {},
      searchParams: {
        pymsm: {
          key: '01'
        }
      },
      targetParams: {
        pymsm: {}
      },
      form: this.$form.createForm(this),
      schemeList: [],
      loadingBtn: false
    }
  },
  computed: {
    ...mapState({
      TYPE0347: state => state.app['TYPE0347'],
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0126: state => state.app['TYPE0126']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0347')
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.$api.base.subjectAllByYx({xklb: '02'}).then(res => {
      this.$set(this.selectMenu, 'subjectList', res.data)
    })
    // this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
      })
    },
    pymsChange(v) {
      this.$set(this.searchParams, 'xkdm', {})
      this.$api.base.subjectAllByYx({xklb: ((v.key === '01' || v.key === '03') ? '02' : '04')}).then(res => {
        this.$set(this.selectMenu, 'subjectList', res.data)
      })
    },
    handleChange (v, k) {
      if (k === 'pyms') {
        this.targetParams.pymsm = v
        this.form.setFieldsValue({targetXkdm: ''})
        this.$api.base.subjectAllByYx({xklb: ((v.key === '01' || v.key === '03') ? '02' : '04')}).then(res => {
          this.$set(this.selectMenu, 'targetSubjectList', res.data)
        })
      }
      // if (k === 'yx') {
        // this.form.setFieldsValue({targetZy: ''})
        // this.$api.base.majorAll({lsdwh: v.key}).then(res => {
        //   this.$set(this.selectMenu, 'targetZyList', res.data)
        // })
      // }
      // if (k === 'pyms') {
      //   this.pymsmc = v.label
      // }
      this.$nextTick(() => {
        this.form.setFieldsValue({targetPyfamc: `${this.form.getFieldValue('targetSsnj') ? this.form.getFieldValue('targetSsnj').label : ''} ${this.form.getFieldValue('targetXslx') ? this.form.getFieldValue('targetXslx').label : ''} ${this.form.getFieldValue('targetXkdm') ? this.form.getFieldValue('targetXkdm').label : ''} ${this.form.getFieldValue('targetXslb') ? this.form.getFieldValue('targetXslb').label : ''}${this.form.getFieldValue('targetPyms') ? (this.form.getFieldValue('targetPyms').key === '03' ? '直博生' : (this.form.getFieldValue('targetPyms').key === '01' ? '一级学科' : '专业学位类别（领域）')) : ''} 培养方案`})
      })
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    schemeChange (index) {
      let targetSch = this.schemeList[index]
      this.targetParams = {...this.searchParams}
      this.$set(this.selectMenu, 'targetSubjectList', this.selectMenu.subjectList)
      this.$nextTick(() => {
        this.form.resetFields()
        this.form.setFieldsValue({
          targetPyms: this.searchParams.pymsm,
          targetXkdm: this.searchParams.xkdm,
          targetXslb: this.searchParams.xslbm,
          // targetXslx: this.searchParams.xslx,
          targetPyfamc: targetSch.pyfamc,
          targetSsnj: this.searchParams.ssnj
        })
      })
    },
    yxChange (code) {
      // this.form.setFieldsValue({sszydm: ''})
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zyList', res.data)
      })
    },
    // targetYxChange (code) {
    //   this.$api.base.majorAll({lsdwh: code}).then(res => {
    //     this.$set(this.selectMenu, 'targetZyList', res.data)
    //   })
    // },
    searchScheme () {
      let param = {}
      for (const key in this.searchParams) {
        param[key] = this.searchParams[key].key
      }
      if (!param.xkdm) {
        this.$message.warning('请选择学科/专业领域')
        return;
      }
      if (!param.ssnj) {
        this.$message.warning('请选择年级')
        return;
      }
      if (!param.xslbm) {
        this.$message.warning('请选择培养类型')
        return;
      }
      this.$api.cultivate.getPyfa(param).then(res => {
        this.schemeList = res.data
        this.searchParams.scheIndex = ''
      })
    },
    submit () {
      this.form.validateFields((err, values) => {

        console.log(values)
        if (err) {
          return
        }
        const sourcePyfaId = this.schemeList[this.searchParams.scheIndex].id
        let params = {
          targetPyms: values.targetPyms.key,
          targetXslb: values.targetXslb.key,
          // targetXslx: values.targetXslx.key,
          targetSsnj: values.targetSsnj.key,
          targetXkdm: values.targetXkdm.key,
          targetPyfamc: values.targetPyfamc,
          sourcePyfaId: sourcePyfaId
        }

        if(!sourcePyfaId){
          this.$message.warning('请选择源培养方案')
          return
        }
        this.loadingBtn = true
        this.$api.cultivate.schemeCopy(params).then(res => {
          this.$message.success(res.msg)
        }).finally(() => {
          this.loadingBtn = false
        })
        // this.$api.cultivate.getTemplate(params).then(() => {
        //
        // })
      })
    }
  }
}
</script>

<style>

</style>
